package tuanda.soict.firstsun.LogicPackage;

import tuanda.soict.firstsun.BuildingPackage.Building;
import tuanda.soict.firstsun.DataPackage.CommonConstants;

/**
 * @author tuanda_soict_hut
 * @version 0.0.3
 * @since 9-9-2012
 */
public class BattleMap implements CommonConstants {

    private MapLocation[][] map;
    private MovementSet[][] movementSet;
    private int numRow;
    private int numColumn;

    public BattleMap(int row, int column) {
        if (row > 0 && column > 0) {
            map = new MapLocation[row][column];
            setNumberOfColumn(column);
            setNumberOfRow(row);
        }
    }

    public final MapLocation[][] getMapLocation() {
        return this.map;
    }

    public final MovementSet[][] getMovementSet() {
        return this.movementSet;
    }

    public final MovementSet getMovementSet(int x, int y) {
        return this.movementSet[y][x];
    }

    public void setMovementSet() {
        this.movementSet = new MovementSet[getNumberOfRow()][getNumberOfColumn()];
    }

    public final void setArmy(Army arm, int x, int y) {
        if (x > -1 && x < numColumn && y > -1 && y < numRow) {
            map[y][x].setArmy(arm);
        }
    }

    public final void setBuilding(Building build, int x, int y) {
        if (x > -1 && x < numColumn && y > -1 && y < numRow) {
            map[y][x].setBuilding(build);
        }
    }

    public final void setArmy(Army arm) {
        if (arm != null) {
            int x = arm.getLocationX();
            int y = arm.getLocationY();
            if (x > -1 && x < numColumn && y > -1 && y < numRow) {
                map[y][x].setArmy(arm);
            }
        }
    }

    public final boolean setEnvironment(int x, int y, String nam, int def,
            int obsPoint, int category) {
        if (x > -1 && x < numColumn && y > -1 && y < numRow) {
            Environment env = new Environment(nam, def, obsPoint, category);
            map[y][x] = new MapLocation(env);
            return true;
        } else {
            return false;
        }
    }

    public final void setBuilding(Building build) {
        if (build != null) {
            int x = build.getLocationX();
            int y = build.getLocationY();
            if (x > -1 && x < numColumn && y > -1 && y < numRow) {
                map[y][x].setBuilding(build);
            }
        }
    }

    public final Environment getEnvironment(int x, int y) {
        if (x > -1 && x < numColumn && y > -1 && y < numRow) {
            return map[y][x].getEnvironment();
        } else {
            return null;
        }
    }

    public final Building getBuilding(int x, int y) {
        if (x > -1 && x < numColumn && y > -1 && y < numRow) {
            return map[y][x].getBuilding();
        } else {
            return null;
        }
    }

    public final Army getArmy(int x, int y) {
        if (x > -1 && x < numColumn && y > -1 && y < numRow) {
            return map[y][x].getArmy();
        } else {
            return null;
        }
    }

    public final int getNumberOfRow() {
        return this.numRow;
    }

    public final void setNumberOfRow(int row) {
        this.numRow = row;
    }

    public final int getNumberOfColumn() {
        return this.numColumn;
    }

    public final void setNumberOfColumn(int column) {
        this.numColumn = column;
    }

    public final boolean isValidCoordinate(int x, int y) {
        if (x > -1 && x < getNumberOfColumn() && y > -1 && y < getNumberOfRow()) {
            return true;
        } else {
            return false;
        }
    }

    public final boolean isValidCoordinate(Coordinate coor) {
        int x = coor.getLocationX();
        int y = coor.getLocationY();
        if (x > -1 && x < getNumberOfColumn() && y > -1 && y < getNumberOfRow()) {
            return true;
        } else {
            return false;
        }
    }
}
